package com.ruoyi.project.common.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Uits {

    /**
     * 合成图片
     * @param tuli 图例图片
     * @param strPic 多张图片的地址
     * @param title 标题
     */
    public static String CreatePic(String tuli,String strPic,String title,String filePath){
        String[] overPath=strPic.split(",");
        int width=725,height=683;
        // 创建BufferedImage对象
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 获取Graphics2D
        Graphics2D g = image.createGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0,width, height);
        try {
            for (String path : overPath) {
                BufferedImage bImage = ImageIO.read(new File(path));
                g.drawImage(bImage, 0, 0, width, height, null);
            }
            BufferedImage bImage = ImageIO.read(new File(tuli));
            g.drawImage(bImage,552, 450, 122, 203, null);
        } catch (IOException e) {
            e.printStackTrace();
        }

        g.setColor(Color.black);
        BasicStroke wideStroke = new BasicStroke(1.5f);
        g.setStroke(wideStroke);
        g.drawRoundRect(1,1,width-2,height-2,0,0);

        //标题
        Font font=new Font("微软雅黑",Font.PLAIN ,23);
        g.setFont(font);
        g.setColor(Color.black);
        //文字
        String title1=title.split(",")[0];
        g.drawString(title1, 256,23);
        font=new Font("仿宋",Font.PLAIN ,20);
        g.setFont(font);
        String title2=title.split(",")[1];
        g.drawString(title2, 222,50);
        String title3="河北省气象台";
        font=new Font("宋体",Font.PLAIN,20);
        g.setFont(font);
        g.drawString(title3, 380,640);
        g.dispose();

        try {
            ImageIO.write(image, "png", new File(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return filePath;
    }

    public static void main(String[] args) {
        CreatePic("D:\\1.jpg","D:\\2.jpg","标题,标题2","D:\\3.png");
    }

}
